//
//  LQGBaseModel.swift
//  LQGBaseModel
//
//  Created by 罗建
//  Copyright (c) 2021 罗建. All rights reserved.
//

import Foundation
import YYModel

/// 基础模型类
open class LQGBaseModel: NSObject {
    
    public required override init() {}
    
    @objc
    private static func modelCustomPropertyMapper() -> [String: Any]? {
        return self.lqg_modelCustomPropertyMapper()
    }
    
    @objc
    private static func modelContainerPropertyGenericClass() -> [String: Any]? {
        return self.lqg_modelContainerPropertyGenericClass()
    }
    
    /// key转换
    open class func lqg_modelCustomPropertyMapper() -> [String: Any]? {
        return nil
    }
    
    /// 模型嵌套
    open class func lqg_modelContainerPropertyGenericClass() -> [String: Any]? {
        return nil
    }
    
    /// 将字典数组转化为模型数组
    /// @param dictionarys 字典数组
    public static func lqg_models<T: LQGBaseModel>(with dictionarys: [[String: Any]]) -> [T] {
        var models: [T] = []
        for dictionary in dictionarys {
            models.append(T.lqg_model(with: dictionary))
        }
        return models
    }

    /// 将字典转化为模型
    /// @param dictionary 字典
    public static func lqg_model(with dictionary: [String: Any]) -> Self {
        let model = self.init()
        model.lqg_modelSet(with: dictionary)
        return model
    }
    
    /// 初始化赋值
    /// @param dictionary 字典
    public convenience init(with dictionary: [String: Any]) {
        self.init()
        self.lqg_modelSet(with: dictionary)
    }
    
    /// 给模型赋值
    /// @param dictionary 字典
    open func lqg_modelSet(with dictionary: [String: Any]) {
        self.yy_modelSet(with: dictionary)
    }

    /// 模型转为字典
    open func lqg_modelToDictionary() -> [String: Any] {
        return (self.yy_modelToJSONObject() as? [String: Any]) ?? [:]
    }
    
}
